<?php
/**
 * Created by PhpStorm.
 * User: guotianqi
 * Date: 15/11/10
 * Time: 18:16
 */

namespace utils;

class HeaderUtils
{
    /**
     * 获取自定义头部变量值
     * @param string $key
     * @return string
     */
    public static function getDiyHeader($key){
        $key = strtoupper($key);
        $key = str_replace("-", "_", $key);
        if(substr($key, 0, strlen("HTTP_"))!="HTTP_"){
            $key="HTTP_".$key;
        }
        $value = isset($_SERVER[$key])?$_SERVER[$key]:'';
        return $value;
    }
	public static function getClientIp()
	{
		if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
		{
			$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		elseif (isset($_SERVER["HTTP_CLIENT_IP"]))
		{
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		}
		elseif (isset($_SERVER["REMOTE_ADDR"]))
		{
			$ip = $_SERVER["REMOTE_ADDR"];
		}
		elseif (getenv("HTTP_X_FORWARDED_FOR"))
		{
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		}
		elseif (getenv("HTTP_CLIENT_IP"))
		{
			$ip = getenv("HTTP_CLIENT_IP");
		}
		elseif (getenv("REMOTE_ADDR"))
		{
			$ip = getenv("REMOTE_ADDR");
		}
		else
		{
			$ip = "";
		}
		return $ip;
	}
    /**
	 * 获取客户端User-Agent信息数组
	 * @return array
	 */
	public static function getUserAgentArr(){
		$strUserAgent = self::getDiyHeader('User-Agent');
		return explode("/", $strUserAgent);
	}
	
	/**
	 * 获取当前请求端的版本信息
	 * @return string
	 */
	public static function getVersion(){
		$arrUserAgent = self::getUserAgentArr();
		return isset($arrUserAgent[1]) ? $arrUserAgent[1] : '';
	}

    	/**
	 * 获取设备唯一识别码
	 * @return string
	 */
	public static function getDeviceToken(){
		$deviceToken = self::getDiyHeader('devicetag');
		//兼容头部没有传递devicetag的字段
		if(empty($deviceToken))
		{
			$deviceToken = self::getDiyHeader('devicetoken');
		}
		return $deviceToken;
	}
	
	/**
	 * 获取会员ID
	 * @return integer
	 */
	public static function getCustomerId(){
		return self::getDiyHeader('customerid');
	}
	
	/**
	 * 获取商家编码
	 * @return string
	 */
	public static function getCompanyCode(){
		return self::getDiyHeader('companycode');
	}

	/**
	 * 获取K米后台用户名称
	 * @return string
	 */
	public static function getKmiBackendUserName(){
		return self::getDiyHeader('CurlUserName');
	}

    /**
	 * 获取当前请求端的产品类型
	 * @return string
	 */
	public static function getProductType(){
	    $arrUserAgent = self::getUserAgentArr();
	    return isset($arrUserAgent[0]) ? $arrUserAgent[0] : ''; 
	}

	/**
	 * 获取当前请求端的操作系统类型
	 * @return string
	 */
	public static function getOsType(){
	    $arrUserAgent = self::getUserAgentArr();
	    return isset($arrUserAgent[3]) ? $arrUserAgent[3] : ''; 
	}

	/**
	 * 获取当前请求源的操作系统
	 * @return number
	 */
	public static function getOsTypeCode(){
		$header = self::getOsType();

		$ostype = -1;
		if(strpos($header, "android")!==false){
			$ostype = 2;
		}elseif(strpos($header, "iphone")!==false){
			$ostype = 0;
		}
		return $ostype;
	}

    /**
     * 获取当前服务器IP
     * @return string
     */
    public static function getServerIP()
    {
        $serverAddr = isset($_SERVER['SERVER_ADDR'])?$_SERVER['SERVER_ADDR']:"";
        return GetHostByName($serverAddr);
    }

	/**
	 * 获取来源 "source":"获取来源 1ios微信 2安卓微信 3iosK米app 4安卓K米app"
	 * @return int
	 */
	public static function getKmSourceId()
	{
		$source = 0;
		$headerOsType = HeaderUtils::getOsType();
		if(HeaderUtils::getProductType() == 'kmdg')
		{
			if($headerOsType == 'android')
			{
				$source = 4;
			}
			else if($headerOsType == 'iphone')
			{
				$source = 3;
			}
		}
		else
		{
			if($headerOsType == 'android')
			{
				$source = 2;
			}
			else if($headerOsType == 'iphone')
			{
				$source = 1;
			}
			else
			{
				$source = 0;
			}
		}
		return $source;
	}
}